﻿namespace Cmn
{
    using System;
    using System.Text;
    using System.Text.RegularExpressions;
    using System.Web;

    public class Str
    {
        public static int cnLength(string str)
        {
            int num = 0;
            foreach (char ch in str)
            {
                if (ch > '\x007f')
                {
                    num += 2;
                }
                else
                {
                    num++;
                }
            }
            return num;
        }

        public static string CutStr(string strInput, int intlen)
        {
            ASCIIEncoding encoding = new ASCIIEncoding();
            int num = 0;
            string str = "";
            byte[] bytes = encoding.GetBytes(strInput);
            for (int i = 0; i < bytes.Length; i++)
            {
                if (bytes[i] == 0x3f)
                {
                    num += 2;
                }
                else
                {
                    num++;
                }
                try
                {
                    str = str + strInput.Substring(i, 1);
                }
                catch
                {
                    break;
                }
                if (num > intlen)
                {
                    break;
                }
            }
            if (Encoding.Default.GetBytes(strInput).Length > intlen)
            {
                str = str + "…";
            }
            return str;
        }

        public static string GetFileExtends(string fileName)
        {
            string str = null;
            if (fileName.IndexOf('.') > 0)
            {
                string[] strArray = fileName.Split(new char[] { '.' });
                str = strArray[strArray.Length - 1];
            }
            return str;
        }

        public static string GetHref(string htmlCode)
        {
            string str = "";
            string pattern = "(h|H)(r|R)(e|E)(f|F) *= *('|\")?((\\w|\\\\|\\/|\\.|:|-|_)+)('|\"| *|>)?";
            foreach (Match match in Regex.Matches(htmlCode, pattern))
            {
                str = str + match.Value.ToLower().Replace("href=", "").Trim() + "||";
            }
            return str;
        }

        public static string GetImg(string imgString, string imgHttp)
        {
            string str = "";
            string pattern = @"src=.+\.(bmp|jpg|gif|png|)";
            foreach (Match match in Regex.Matches(imgString.ToLower(), pattern))
            {
                str = str + match.Value.ToLower().Trim().Replace("src=", "");
            }
            return (imgHttp + str);
        }

        public static string GetImgSrc(string htmlCode, string imgHttp)
        {
            string str = "";
            string pattern = "<img.+?>";
            foreach (Match match in Regex.Matches(htmlCode, pattern))
            {
                str = str + GetImg(match.Value.ToLower().Trim(), imgHttp) + "||";
            }
            return str;
        }

        public static string GetRegexValue(string regexString, string htmlCode)
        {
            string str = "";
            Match match = new Regex(regexString).Match(htmlCode);
            if (match.Success)
            {
                str = match.Value;
            }
            return str;
        }

        public static string GetStr(string str, int length, bool flg)
        {
            int num = 0;
            int num2 = 0;
            foreach (char ch in str)
            {
                if (ch > '\x007f')
                {
                    num += 2;
                }
                else
                {
                    num++;
                }
                if (num > length)
                {
                    str = str.Substring(0, num2);
                    if (flg)
                    {
                        str = str + "...";
                    }
                    return str;
                }
                num2++;
            }
            return str;
        }

        public static string GetSubString(string p_SrcString, int p_Length, string p_TailString)
        {
            string str = p_SrcString;
            if (p_Length < 0)
            {
                return str;
            }
            byte[] bytes = Encoding.Default.GetBytes(p_SrcString);
            if (bytes.Length <= p_Length)
            {
                return str;
            }
            int length = p_Length;
            int[] numArray = new int[p_Length];
            byte[] destinationArray = null;
            int num2 = 0;
            for (int i = 0; i < p_Length; i++)
            {
                if (bytes[i] > 0x7f)
                {
                    num2++;
                    if (num2 == 3)
                    {
                        num2 = 1;
                    }
                }
                else
                {
                    num2 = 0;
                }
                numArray[i] = num2;
            }
            if ((bytes[p_Length - 1] > 0x7f) && (numArray[p_Length - 1] == 1))
            {
                length = p_Length + 1;
            }
            destinationArray = new byte[length];
            Array.Copy(bytes, destinationArray, length);
            return (Encoding.Default.GetString(destinationArray) + p_TailString);
        }

        public static string HtmlDecode(string strInput)
        {
            return HttpContext.Current.Server.HtmlDecode(strInput);
        }

        public static string HtmlEncode(string strInput)
        {
            return HttpContext.Current.Server.HtmlEncode(strInput);
        }

        public static bool isMatch(string op, string str)
        {
            if (str.Equals(string.Empty) || (str == null))
            {
                return false;
            }
            Regex regex = new Regex(op, RegexOptions.IgnoreCase);
            return regex.IsMatch(str);
        }

        public static string RegReplace(string regValue, string regStart, string regEnd)
        {
            string str = regValue;
            if ((regValue != "") && (regValue != null))
            {
                if ((regStart != "") && (regStart != null))
                {
                    str = str.Replace(regStart, "");
                }
                if ((regEnd != "") && (regEnd != null))
                {
                    str = str.Replace(regEnd, "");
                }
            }
            return str;
        }

        public static string RemoveHTML(string htmlCode)
        {
            string input = htmlCode;
            input = new Regex("<br>", RegexOptions.IgnoreCase).Replace(input, "\n");
            foreach (Match match in Regex.Matches(htmlCode, "<[^{><}]*>"))
            {
                input = input.Replace(match.Value, "");
            }
            return new Regex("\n", RegexOptions.IgnoreCase).Replace(input, "<br>");
        }

        public static string ReplaceEnter(string htmlCode)
        {
            string str = "";
            if ((htmlCode == null) || (htmlCode == ""))
            {
                str = "";
            }
            else
            {
                str = htmlCode.Replace("\"", "");
            }
            return str.Replace("\r\n", "");
        }

        public static int ToInt(object strValue, int defValue)
        {
            if (((strValue == null) || (strValue.ToString() == string.Empty)) || (strValue.ToString().Length > 10))
            {
                return defValue;
            }
            string str = strValue.ToString();
            string strNumber = str[0].ToString();
            if (((str.Length == 10) && Valid.isNumber(strNumber)) && (int.Parse(strNumber) > 1))
            {
                return defValue;
            }
            if (!((str.Length != 10) || Valid.isNumber(strNumber)))
            {
                return defValue;
            }
            int num = defValue;
            if ((strValue != null) && new Regex("^([-]|[0-9])[0-9]*$").IsMatch(strValue.ToString()))
            {
                num = Convert.ToInt32(strValue);
            }
            return num;
        }

        public static string UrlEncoding(string str)
        {
            byte[] bytes = Encoding.UTF8.GetBytes(str);
            return Encoding.UTF8.GetString(bytes).ToString();
        }
    }
}

